
package com.zcp.notepad;

import android.content.DialogInterface;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.ArrayList;

public class NoteDeleteActivity extends SelectList {
    /**
     * 处理done按钮点击事件
     */
    @Override
    protected void dealDoneOnClick() {
        checkPassword();
    }

    @Override
    protected void dealCancelOnClick() {
        finish();

    }

    private void checkPassword() {
        ArrayList<HashMap<String, Object>> value = getCheckObject();
        if(value.size()<=0)
        {
            return;
        }
        boolean shouldCheckPassword = false;
        int lock = NotePad.DataValue.IS_UNLOCK;
        for (HashMap<String, Object> d : value) {
            lock = Integer.valueOf(d.get(NotePad.Notes.LOCK).toString());
            if (NotePad.DataValue.IS_LOCK == lock) {
                shouldCheckPassword = true;
                break;
            }
        }

        if (shouldCheckPassword) {
            if (myInterfaceOkCancelClick == null) {
                myInterfaceOkCancelClick = new MyInterfaceOkCancelClick();
            }
            Util.getCheckPasswordDialog(this, getString(R.string.Delete), null,
                    myInterfaceOkCancelClick,null).show();
        } else {
            showCheckDeleteMessage();
        }

    }

    private void showCheckDeleteMessage() {
        DialogInterface.OnClickListener okInface = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                deleteData();
            }
        };

        String title = getString(R.string.Delete);
        String message = getString(R.string.delete_check_message, getCheckObject().size());
        Util.getOkCancelDialog(this, title, message, null, okInface).show();
    }

    private void showText(int StringId) {
        Toast.makeText(this, getString(StringId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 处理弹出对话框的ok按钮点击事件。
     */
    private void deleteData() {
        deleteSelect();
    }

    class MyInterfaceOkCancelClick implements InterfaceOkCancelClick {

        @Override
        public void dealWithOk(boolean isCheckOk, boolean valid, DialogInterface dialog,TextView passwordTv,Object tagObject) {
            // 密码无效情况下。
            if (!valid) {
                Util.setDialogCanDismiss(dialog, false);
                dialog.dismiss();
                passwordTv.setText("");
                showText(R.string.invalid_password);
                return;
            }

            if (isCheckOk) {
                Util.setDialogCanDismiss(dialog, true);
                dialog.dismiss();
                showCheckDeleteMessage();
            } else {
                Util.setDialogCanDismiss(dialog, false);
                dialog.dismiss();
                passwordTv.setText("");
                showText(R.string.wrong_password);
            }
        }

        @Override
        public void dealWithCancel(DialogInterface dialog,Object tagObject) {
            Util.setDialogCanDismiss(dialog, true);
            dialog.dismiss();
        }
    }

    private MyInterfaceOkCancelClick myInterfaceOkCancelClick;
}
